package org.zhuyx.ch02.shili.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhuyx.ch02.shili.dao.PetDao;
import org.zhuyx.ch02.shili.dao.impl.PetDaoMysqlImpl;
import org.zhuyx.ch02.shili.dao.impl.PetDaoOracleImpl;
import org.zhuyx.ch02.shili.entity.Pet;
import org.zhuyx.ch02.shili.service.PetService;

import javax.annotation.Resource;

/**
 * @author 茜茜
 * @date 2024-12-05 11:23
 */
@Service("petService")
public class PetServiceImpl implements PetService {
    //简单工厂获取
//    PetDao petDao=new PetDaoMysqlImpl();
    @Resource(type = PetDaoOracleImpl.class)
    private PetDao petDao;

    public PetDao getPetDao() {
        return petDao;
    }

    public void setPetDao(PetDao petDao) {
        this.petDao = petDao;
    }

    @Override
    public boolean insert(Pet pet) {
        int rows=petDao.insert(pet);
        delay(3000);
//        if(true){
//            throw new RuntimeException("业务操作异常！");
//        }
        return rows > 0;
    }

    private void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean modify(Pet pet) {
        delay(2000);
        return false;
    }

    @Override
    public boolean del(Integer id) {
        delay(1000);
        return false;
    }
}
